﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.Collections.ObjectModel;

public class GetInventoryList : MonoBehaviour
{
    public static List<Inventory> inventoryList = new List<Inventory>();

    private static Dictionary<string, Inventory> inventoryDic = new Dictionary<string, Inventory>();

    public static ReadOnlyDictionary<string, Inventory> InventoryDic = new ReadOnlyDictionary<string, Inventory>(inventoryDic);

    private void Start()
    {
        GetInventroy();
    }
    private void GetInventroy()
    {
        TextAsset textAsset = Resources.Load<TextAsset>("ItemJson");
        JsonData json = JsonMapper.ToObject(textAsset.text);

        for (int i = 0; i < json.Count; i++)
        {
            int id = (int)json[i]["id"];
            string name = (string)json[i]["name"];
            Type type = (Type)System.Enum.Parse(typeof(Type), (string)json[i]["type"]);
            int capacity = (int)json[i]["capacity"];
            int num = (int)json[i]["num"];
            string sprite = (string)json[i]["sprite"];
            string des = (string)json[i]["des"];

            Inventory inventory = null;
            switch (type)
            {
                case Type.Weapon:
                    WeaponType weaponType = (WeaponType)System.Enum.Parse(typeof(WeaponType), (string)json[i]["weaponType"]);
                    int attack = (int)json[i]["attack"];

                    inventory = new Weapon(id, name, type, capacity, num, sprite, des, weaponType, attack);
                    break;
                case Type.Comsuable:
                    int hp = (int)json[i]["hp"];
                    int mp = (int)json[i]["mp"];

                    inventory = new Comsuable(id, name, type, capacity, num, sprite, des, hp, mp);
                    break;
                case Type.Prop:
                    break;
            }
            inventoryList.Add(inventory);
            inventoryDic.Add(name, inventory);
        }
    }
}